- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathsend_mail.py
30 lines (28 loc) · 993 Bytes
/
send_mail.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
importsmtplib
fromemail.mime.textimportMIMEText
fromemail.mime.multipartimportMIMEMultipart
# environment variables
username='hungrypy@gmail.com'
password='LetsGetItStarted2020'
defsend_mail(text='Email Body', subject='Hello World', from_email='Hungry Py <hungrypy@gmail.com>', to_emails=None, html=None):
assertisinstance(to_emails, list)
msg=MIMEMultipart('alternative')
msg['From'] =from_email
msg['To'] =", ".join(to_emails)
msg['Subject'] =subject
txt_part=MIMEText(text, 'plain')
msg.attach(txt_part)
ifhtml!=None:
html_part=MIMEText(html, 'html')
msg.attach(html_part)
msg_str=msg.as_string()
# login to my smtp server
server=smtplib.SMTP(host='smtp.gmail.com', port=587)
server.ehlo()
server.starttls()
server.login(username, password)
server.sendmail(from_email, to_emails, msg_str)
server.quit()
# with smtplib.SMTP() as server:
# server.login()
# pass